package com.judysen.jsqlweb.filters;

import com.judysen.jsqlweb.models.ThreadContextModel;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 线程过滤器，在所有的过滤器最前面执行
 */
@Component
public class ThreadContext implements Filter {

    private static InheritableThreadLocal<ThreadContextModel> threadLocal = new InheritableThreadLocal<>();

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        setThreadLocalModel(servletRequest, servletResponse);
        filterChain.doFilter(servletRequest, servletResponse);
        threadLocal.remove();
        threadLocal.set(null);
    }

    /**
     * @param servletRequest
     * @param servletResponse
     */
    private void setThreadLocalModel(ServletRequest servletRequest, ServletResponse servletResponse) {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        System.out.println(request.getServletPath());

        ThreadContextModel contextModel = new ThreadContextModel();
        contextModel.setResponse(response);
        contextModel.setRequest(request);
        //----从request header中获取数据库Key---'database'
        String dsName = request.getHeader("database");
        if (StringUtils.isEmpty(dsName)) {
            dsName = "default_web";
        }
        contextModel.setDsName(dsName);                             //将dsName注入
        threadLocal.set(contextModel);
    }

    /**
     * 返回当前的request
     *
     * @return
     */
    public static HttpServletRequest getRequest() {
        var threadModel = threadLocal.get();
        if (threadModel != null) {
            return threadModel.getRequest();
        } else {
            return null;
        }
    }

    /**
     * 返回当前的response
     *
     * @return
     */
    public static HttpServletResponse getResponse() {
        var threadModel = threadLocal.get();
        if (threadModel != null) {
            return threadModel.getResponse();
        } else {
            return null;
        }
    }

    /**
     * 获取数据库定义名
     *
     * @return
     */
    public static String getDatabaseName() {
        var threadModel = threadLocal.get();
        if (threadModel != null) {
            return threadModel.getDsName();
        } else {
            return null;
        }
    }

    /**
     * 测试用的，其他环境不可使用此方法
     * @param displayName
     */
    public static void setDisplayName(String displayName){
        var model=threadLocal.get();
        if(model==null){
            model=new ThreadContextModel();
        }
        model.setDsName(displayName);
        threadLocal.set(model);
    }
}
